//
//  InputBillViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/5.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class InputBillViewController: BillRootViewController {
    
    fileprivate var inputBillView : InputBillView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = inputBill
        addSubView()
    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        inputBillView = InputBillView.init(frame: self.view.bounds)
        inputBillView.delegate = self
        self.view.addSubview(inputBillView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


}

extension InputBillViewController : InputBillViewDelegate {
    
    //MAKR: 选择操作类型
    func selectOperate(index: Int) {
        
        switch index {
        case 1:
            let dataArray = [BankofChina,Constructionbank,ICBC,ABC,CITICBank,BankofCommunications,GuangfaBank,EverbrightBank,HSBCBank,MinshengBank,PufaBank,PostalBank,ChinaMerchantsBank,IndustrialBank,HenanRuralCredit,HSBC,BankofEastAsia,HuishangBank,ShanghaiRuralCommercialBank,GuangzhouBank,BankofBeijing,CitiBank,BaoshangBank,HangzhouBank,ShanghaiBank]
            showPickView(dataArray: dataArray, selectText: self.inputBillView.bankView.text) {[weak self] (text) in
                self?.inputBillView.bankView.text = text
            }
        case 2:
            showDatePickView {[weak self] (date) in
                self?.inputBillView.billDateView.text = date
            }
        case 3:
            showDatePickView {[weak self] (date) in
                self?.inputBillView.repaymentView.text = date
            }
        case 4:
            AddBillAction()
        default:
            break
        }
    }
    
    //MARK: 显示选择框
    fileprivate func showPickView(dataArray:[String],selectText:String,block:@escaping (String)->()) {
        
        let selectTypeVC = SelectTypeViewController.init(dataArray: dataArray, selectText: selectText)
        selectTypeVC.modalPresentationStyle = .overCurrentContext
        selectTypeVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        selectTypeVC.textBlock = { (text) in
            block(text)
        }
        self.present(selectTypeVC, animated: false, completion: nil)
    }
    
    //MARK: 显示日期选择框
    fileprivate func showDatePickView(block:@escaping (String)->()) {
        
        let selectDateVC = SelectDateViewController()
        selectDateVC.modalPresentationStyle = .overCurrentContext
        selectDateVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        selectDateVC.dateBlock = {(date) in
            block(date)
        }
        self.present(selectDateVC, animated: false, completion: nil)
    }
    
    //MARK: 添加账单
    @objc fileprivate func AddBillAction() {
        
        if (self.inputBillView.numberView.textView.text as NSString?)?.length != 4 {
            
            KMTool.showText(_text: inputBankCardNumberOfFour)
            return
        }
        
        if ((self.inputBillView.nameView.textView.text as NSString?)?.length)! > 5 {
            
            KMTool.showText(_text: nameNotGreate)
            return
        }
        
        if Int(self.inputBillView.billLinesView.textView.text!) ?? 1000 > 10000 || Int(self.inputBillView.cardLinesView.textView.text!) ?? 1000 > 10000 {
            
            KMTool.showText(_text: limitNotGreate)
            return
        }
        
        var name = self.inputBillView.nameView.textView.text
        if name == "" {
            name = KMTool.stringForEnumKey(.userName) ?? "张三"
        }
        
        var cardLines = self.inputBillView.cardLinesView.textView.text
        if cardLines == "" {
            cardLines = "1000"
        }
        
        var billLines = self.inputBillView.billLinesView.textView.text
        if billLines == "" {
            billLines = "1000"
        }
        
        if self.inputBillView.billDateView.text.getTimestamp() > self.inputBillView.repaymentView.text.getTimestamp() {
            
            KMTool.showText(_text: billDateMustBeforRepaymentDate)
            return
        }
        
        if self.inputBillView.cardLinesView.textView.text! < self.inputBillView.billLinesView.textView.text! {
            
            KMTool.showText(_text: cardLinesMustGreatethanBillLines)
            return
        }
        
        let userInfo = ["number":self.inputBillView.numberView.textView.text!,"bank":self.inputBillView.bankView.text!,"name":name!,"repaymentDate":self.inputBillView.repaymentView.text!,"billDate":self.inputBillView.billDateView.text!,"cardLines":cardLines!,"billLines":billLines!,"type":"bank"]
        
        NotificationCenter.default.post(name: .addBillPush, object: nil, userInfo: userInfo)
    
    self.navigationController?.popToRootViewController(animated: true)
    }
    
    
}
